package com.finer.service.warehouse.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.finer.dao.warehouse.WarehouseInfoDao;
import com.finer.dto.warehouse.WarehouseInfoPageDTO;
import com.finer.entity.ResponseMessage;
import com.finer.entity.stock.FinerWarehouse;
import com.finer.service.warehouse.WarehouseInfoService;
import com.finer.utils.PageUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author wangenhao
 * @className WarehouseInfoServiceImpl
 * @description 仓库信息service实现类
 * @date 2024-10-23 下午5:11
 **/
@Service
@Slf4j
public class WarehouseInfoServiceImpl implements WarehouseInfoService {

    @Autowired
    private WarehouseInfoDao warehouseInfoDao;

    /**
     * @return com.finer.entity.ResponseMessage
     * @description 获取仓库信息分页
     * @date 2024/10/23 下午5:23
     * @Param warehouseInfoPageDTO:
     **/
    public ResponseMessage getWarehouseInfoPage(WarehouseInfoPageDTO warehouseInfoPageDTO) {
        int pageNum = 1;
        int pageSize = 10;
        if (warehouseInfoPageDTO.getPageNum() != null) {
            pageNum = warehouseInfoPageDTO.getPageNum();
        }
        if (warehouseInfoPageDTO.getPageSize() != null) {
            pageSize = warehouseInfoPageDTO.getPageSize();
        }
        if (StringUtils.isBlank(warehouseInfoPageDTO.getHouseCode()) && StringUtils.isBlank(warehouseInfoPageDTO.getHouseName())) {
            warehouseInfoPageDTO.setParentId(0);
        }
        Page<FinerWarehouse> page = PageHelper.startPage(pageNum, pageSize);
        List<FinerWarehouse> warehouseInfoList = warehouseInfoDao.getWarehouseInfoList(warehouseInfoPageDTO);
        List<FinerWarehouse> warehouseInfoChildrenList = new ArrayList<>();
        for (FinerWarehouse warehouse : warehouseInfoList) {
            warehouseInfoChildrenList.addAll(getWarehouseInfoChilrenList(warehouse.getId()));
            log.info("warehouseInfoChildrenList: {}", warehouseInfoChildrenList);
        }
        warehouseInfoList.addAll(warehouseInfoChildrenList);
        List<FinerWarehouse> resList = warehouseInfoList
                .stream()
                .filter(finerWarehouse -> {
                    return Objects.equals(finerWarehouse.getParentId(), 0);
                })
                .map(finerWarehouse -> {
                    finerWarehouse.setChildren(getChildrenList(finerWarehouse.getId(), warehouseInfoList));
                    return finerWarehouse;
                })
                .collect(Collectors.toList());
        log.info("resList: {}", resList);
        log.info("WarehouseInfoServiceImpl getWarehouseInfoPage pageNum: {}, pageSize: {}, total: {}, pages: {}, data: {}", pageNum, pageSize, page.getTotal(), page.getPages(), warehouseInfoList);
        PageUtil<FinerWarehouse> pageUtil = new PageUtil<>();
        pageUtil.setPageNum(pageNum);
        pageUtil.setPageSize(pageSize);
        pageUtil.setTotal(page.getTotal());
        pageUtil.setPages(page.getPages());
        pageUtil.setData(resList);
        return ResponseMessage.success(pageUtil);
    }

    /**
     * @return com.finer.entity.ResponseMessage
     * @description 添加仓库信息
     * @date 2024/10/23 下午6:04
     * @Param finerWarehouse:
     **/
    public ResponseMessage addWarehouseInfo(FinerWarehouse finerWarehouse) {
        if (StringUtils.isBlank(finerWarehouse.getHouseName())) {
            return ResponseMessage.error("500", "仓库名称不能为空");
        }
        //TODO 仓库编码校验重复
        if (StringUtils.isBlank(finerWarehouse.getHouseCode())) {
            return ResponseMessage.error("500", "仓库编码不能为空");
        }
        if (finerWarehouse.getParentId() == null) {
            finerWarehouse.setParentId(0);
        }
        finerWarehouse.setCreateBy(1);
        finerWarehouse.setCreateDate(new Date());
        int addNum = warehouseInfoDao.addGoodsType(finerWarehouse);
        log.info("WarehouseInfoServiceImpl addWarehouseInfo finerWarehouse: {} addNum:{}", finerWarehouse, addNum);
        if (addNum > 0) {
            return ResponseMessage.success();
        }
        return ResponseMessage.error("500", "添加失败");
    }

    /**
     * @return com.finer.entity.ResponseMessage
     * @description 根据id获取仓库信息
     * @date 2024/10/23 下午6:04
     * @Param id:
     **/
    public ResponseMessage getWarehouseInfoById(Integer id) {
        if (id == null) {
            return ResponseMessage.error("500", "仓库id不能为空");
        }
        FinerWarehouse finerWarehouse = warehouseInfoDao.getWarehouseInfoById(id);
        log.info("WarehouseInfoServiceImpl getWarehouseInfoById id:{}, finerWarehouse:{}", id, finerWarehouse);
        return ResponseMessage.success(finerWarehouse);
    }

    /**
     * @return com.finer.entity.ResponseMessage
     * @description 检查仓库编码是否重复
     * @date 2024/10/23 下午6:04
     * @Param houseCode:
     **/
    public ResponseMessage checkHouseCode(String houseCode) {
        //分类编号不能为空
        if (StringUtils.isBlank(houseCode)) {
            return ResponseMessage.error("500", "仓库编码不能为空");
        }

        //根据分类编号获取分类编码数量
        int count = warehouseInfoDao.getWarehouseInfoCountByCode(houseCode);
        log.info("WarehouseInfoServiceImpl checkHouseCode houseCode: {} count:{}", houseCode, count);

        //如果数量大于0，则说明分类编码已存在
        if (count > 0) {
            return ResponseMessage.error("500", "仓库编码已存在,请重新刷新!");
        }
        return ResponseMessage.success();
    }

    /**
     * @return com.finer.entity.ResponseMessage
     * @description 更新仓库信息
     * @date 2024/10/23 下午6:04
     * @Param finerWarehouse:
     **/
    public ResponseMessage updateWarehouseInfo(FinerWarehouse finerWarehouse) {
        if (finerWarehouse.getId() == null) {
            return ResponseMessage.error("500", "仓库id不能为空");
        }
        if (StringUtils.isBlank(finerWarehouse.getHouseName())) {
            return ResponseMessage.error("500", "仓库名称不能为空");
        }
        finerWarehouse.setModifyDate(new Date());
        finerWarehouse.setModifyBy(1);
        int updNum = warehouseInfoDao.updateWarehouseInfo(finerWarehouse);
        log.info("WarehouseInfoServiceImpl updateWarehouseInfo finerWarehouse: {} updNum:{}", finerWarehouse, updNum);
        if (updNum > 0) {
            return ResponseMessage.success();
        }
        return ResponseMessage.error("500", "更新失败");
    }

    /**
     * @return com.finer.entity.ResponseMessage
     * @description 删除仓库信息
     * @date 2024/10/23 下午6:05
     * @Param id:
     **/
    public ResponseMessage deleteWarehouseInfo(Integer id) {
        //TODO: 校验仓库中是否有商品
        FinerWarehouse finerWarehouse = new FinerWarehouse();
        finerWarehouse.setId(id);
        finerWarehouse.setIsDel(1);
        finerWarehouse.setModifyBy(1);
        finerWarehouse.setModifyDate(new Date());
        int delNum = warehouseInfoDao.updateWarehouseInfo(finerWarehouse);
        log.info("WarehouseInfoServiceImpl updateWarehouseInfo id: {} delNum:{}", id, delNum);
        if (delNum > 0) {
            return ResponseMessage.success();
        }
        return ResponseMessage.error("500", "删除失败");
    }


    /**
     * @return java.util.List<com.finer.entity.stock.FinerWarehouse>
     * @description 从数据库递归获取子仓库
     * @date 2024/10/23 下午5:43
     * @Param parentId:
     **/
    public List<FinerWarehouse> getWarehouseInfoChilrenList(Integer parentId) {
        List<FinerWarehouse> res = new ArrayList<>();
        List<FinerWarehouse> childrenList = null;
        WarehouseInfoPageDTO warehouseInfoPageDTO = new WarehouseInfoPageDTO();
        warehouseInfoPageDTO.setParentId(parentId);
        childrenList = warehouseInfoDao.getWarehouseInfoList(warehouseInfoPageDTO);
        if (childrenList != null && !childrenList.isEmpty()) {
            res.addAll(childrenList);
            for (FinerWarehouse warehouse : childrenList) {
                res.addAll(getWarehouseInfoChilrenList(warehouse.getId()));
            }
        }
        return res;
    }


    /**
     * @return java.util.List<com.finer.entity.stock.FinerWarehouse>
     * @description 递归获取子仓库
     * @date 2024/10/23 下午5:43
     * @Param parentId:
     * @Param warehouseInfoList:
     **/
    public List<FinerWarehouse> getChildrenList(Integer parentId, List<FinerWarehouse> warehouseInfoList) {
        List<FinerWarehouse> childrenList = warehouseInfoList
                .stream()
                .filter(warehouseInfo -> {
                    return Objects.equals(warehouseInfo.getParentId(), parentId);
                })
                .map(warehouseInfo -> {
                    warehouseInfo.setChildren(getChildrenList(warehouseInfo.getId(), warehouseInfoList));
                    return warehouseInfo;
                })
                .collect(Collectors.toList());
        log.info("childrenList: {}", childrenList);
        return childrenList;
    }
}
